﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Http;
using System.Configuration;
using System.Web.Routing;
using ExpenseSystem.Common;
using System.Web.SessionState;
using ExpenseSystem.Lor;

namespace ExpenseSystem
{
    public class WebApiApplication : System.Web.HttpApplication
    {
        public override void Init()
        {
            this.PostAuthenticateRequest += (sender, e) => HttpContext.Current.SetSessionStateBehavior(SessionStateBehavior.Required);
            base.Init();
        }
        public void Application_BeginRequest()
        {
            HttpContext.Current.Items[Definition.SESSION_TRAN_STR] = SqlProvider.CreateTransaction();

            //UserProfile userProfile = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
            //LogHelper.Info(this.GetType().Name, String.Format("用户{0}正从{1}访问{2}", userProfile == null ? "anonymous" : userProfile.UserCode, Request.UserHostAddress, Request.Url.ToString()));
        }
        public void Application_EndRequest()
        {
            SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString());

        }

        public void Session_Start()
        {
            UserProfile userProfile = HttpContext.Current.Session[Definition.SESSION_LOGIN_STR] as UserProfile;
            LogHelper.Info(this.GetType().Name, String.Format("用户{0}正从{1}访问{2}", userProfile == null ? "anonymous" : userProfile.UserCode, Request.UserHostAddress, Request.Url.ToString()));
        }
        public void Session_End()
        {

        }

        public void Application_Error()
        {
            SqlProvider.CloseTransaction(HttpContext.Current.Items[Definition.SESSION_TRAN_STR].ToString(),false);
        }
        protected void Application_Start()
        {
            GlobalConfiguration.Configure(WebApiConfig.Register);
            Variable.EXEC_PATH = Server.MapPath("./");
            bool logInited = LogHelper.Init();
            if (!logInited)
            {
                Variable.INNER_ERROR = "日志初始化失败，系统无法启动";
                return;
            }
            LogHelper.Info(GetType().Name,"************************系统正在启动*************************");
            try
            {
                Variable.DB_CONN = ConfigurationManager.ConnectionStrings["es"].ConnectionString;
                int initVal = 60000;
                Int32.TryParse(ConfigurationManager.AppSettings["KILL_TRANS_PERIOD"],out initVal);
                Variable.KILL_TRANS_PERIOD = initVal;
                initVal = 60000;
                Int32.TryParse(ConfigurationManager.AppSettings["TRANS_TTL"], out initVal);
                Variable.TRANS_TTL = initVal;


                LogHelper.Info(GetType().Name, "读取配置信息成功");
            }
            catch(Exception ex)
            {
                LogHelper.Info(GetType().Name,String.Format( "读取连接字符串失败：{0}",ex.Message));
                Variable.INNER_ERROR = ex.Message;
            }
        }
    }
}
